/**
 * @file task_service.c
 * @brief
 * @author  ()
 * @version 1.0
 * @date 2024-09-13
 *
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-13 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "cmsis_os2.h"
#include "global.h"

#include "include/port/swei_log.h"
#include "include/swei_socket.h"
#include "include/swei_login.h"
#include "include/swei_uplink.h"
#include "include/swei_downlink.h"
#include "include/swei_config.h"
#include "include/swei_alarm.h"
#include "include/swei_audio.h"
#include "include/swei_active.h"
#include "include/swei_ota.h"
#include "include/swei_ble.h"
#include "include/swei_cmd.h"
#include "include/port/swei_wrapper.h"
#include "include/swei_l3_pattern.h"

#include "include/port/swei_fw_info.h"
#include "FreeRTOS.h"
#include "task.h"
#include "comm/co_heartbeat.h"
// #include "semphr.h"

static TaskHandle_t service_handle;
static SemaphoreHandle_t service_mutex = NULL; /* 信号量保护 */

extern void swei_ble_init(void);
extern void swei_audio_init(void);

void task_service_entry(void *param);
void task_service_init(void);
void task_service_process(void);
void task_service_deinit(void);

void serviceSemaphoreGive(void)
{
    xSemaphoreGive(service_mutex);
}

int serviceSemaphoreTake(void)
{
    if (NULL == service_mutex) {
        taskENTER_CRITICAL();
        if (NULL == service_mutex)
            service_mutex = xSemaphoreCreateMutex();
        taskEXIT_CRITICAL();
    }
    if (NULL == service_mutex)
        return -ENOMEM;
    if (pdFALSE == xSemaphoreTake(service_mutex, portMAX_DELAY))
        return -EBUSY;
    return 0;
}


/// @brief 服务连接入口函数
/// @return  0 - service 任务正常运行
int32_t qltask_service_start(void)
{
    // Create Service Thread with routine function service_routine;
    if (pdPASS != xTaskCreate(task_service_entry, "service", TASKSTACK_SERVICE, NULL, TASKPRI_SERVICE, &service_handle))
    {
        Error_Handler();
    }

    return 0;
}

/// @brief 服务器线程
/// @param
void task_service_entry(void *param)
{
    task_service_init();

    while (1)
    {
        // if(tbox_dqa_mode_get())
        // {
        //     dqa_process();       // DQA mode didn`t run the server process;
        // }
        // else
        {
            task_service_process();
        }

        osDelay(100); // TODO: need covert tick to ms
    }

    // task_service_deinit();
    // vTaskDelete(NULL);
}

/// @brief 服务器处理流程初始化
/// @param
void task_service_init(void)
{
    /* 1. 读取设备配置信息 */
    //swei_config_init(); // 调整到flash_main.c中，上电第一时间读取配置信息
    /* 2. 初始化告警队列 */
    swei_alarm_init();
    /* 2. 套接字初始化 */
    swei_socket_init();
    /* 3. login initial */
    swei_login_init();
    /* 4. uplink_initial */
    swei_uplink_init();
    /* 5. downlink_initial */
    swei_downlink_init();
    /* 6. downlink cmd initial */
    swei_downlink_cmd_init();
    /* 7. ota initial */
    swei_ota_init();
    /* 8. audio initial */
    swei_audio_init();
    /* 8. ble initial */
    swei_ble_init();
    
    swei_l3_lib_module_init();
}

/// @brief 服务器处理流程
/// @param
void task_service_process(void)
{
    swei_gen_workid();
    
    if(!swei_config_ready()) {
        osDelay(5000);
        return;
    }
    
    /* 2. 连接套接字管理 */
    swei_socket_process();
    /* 3. login & heartbeat */
    swei_login_process();
    /* 4. uplink service */
    swei_uplink_process();
    /* 5. downlink service */
    swei_downlink_process();
    /* 7. 定期ota */
    swei_ota_rtc_check();
}

/// @brief 服务器处理流程初始化
/// @param
void task_service_deinit(void)
{
    /* 1. 上行数据去初始化 */
    swei_uplink_deinit();
    /* 2. 下行数据去初始化 */
    swei_downlink_deinit();
    /* 3. 服务器相关数据去初始化 */
    swei_login_deinit();
    /* 4. 套接字功能去初始化 */
    // mmgr_socket_free();
    /* 5. 清空本地连接参数(key、sn、token等不包含证书)*/
    // mdm_param_load_free();
}
